﻿--Local
local spellnameH = GetSpellInfo(136)
local spellnameF = GetSpellInfo(6991)

--Frame
local f = CreateFrame("Frame","ESN_Pet_Care",PetFrameHappiness)
f:SetFrameStrata("BACKGROUND")
f:SetWidth(20)
f:SetHeight(20)
f:ClearAllPoints()
f:SetPoint("TOPLEFT", PetFrameHappiness, "TOPRIGHT", 4, -2)

--Cooldown
local cooldown = CreateFrame("cooldown", nil, f, "CooldownFrameTemplate")
cooldown:SetPoint("CENTER", 0, -1)
cooldown:SetWidth(20)
cooldown:SetHeight(20)
cooldown:Hide()

--Texture 
local t1 = f:CreateTexture(nil,"BACKGROUND")
local t2 = f:CreateTexture(nil,"BACKGROUND")
	
--Register Events
f:RegisterEvent("UNIT_AURA")

--Script
f:SetScript("OnEvent", function(frame, event, name)
	if event == "UNIT_AURA" then
		if arg1 == "pet" then
			f:Hide()
			local buff,heal,feed,duration,expirationTime,start
			for i=1,16 do
				buff, _, _, _, _, duration, expirationTime, _, _ = UnitBuff("pet",i)
				if not buff then
					break
					elseif buff == spellnameH then
					heal = true
					start = expirationTime - duration
					CooldownFrame_SetTimer(cooldown, start, duration, 1)
				elseif buff == spellnameF then
					feed = true
					start = expirationTime - duration
					CooldownFrame_SetTimer(cooldown, start, duration, 1)
				end
			end
			if heal then
				t2:SetTexture()
				t1:SetTexture("Interface\\Icons\\Ability_Hunter_MendPet")
				t1:SetAllPoints(f)
				f.texture = t1
				f:Show()
				heal = false
			elseif feed then
				t1:SetTexture()
				t2:SetTexture("Interface\\Icons\\Ability_Hunter_BeastTraining")
				t2:SetAllPoints(f)
				f.texture = t2
				f:Show()
				feed = false
			end
		end
	end
end)
